<template>
  <div class="login" id="login">
    <div class="login-wrap">
      <!-- <img src="@/assets/images/SCHOOL.png" alt="" width="45.8125rem" height="31.0625rem"> -->
      <div class="login-logo"></div>
      <div class="login-box">
        <div class="sys-title">{{school}}安全用电管理平台</div>
        <div class="sys-welcome">WELCOME!</div>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <!-- <h3 class="title">沈阳化工大学安全用电管理平台</h3> -->
          <div class="form-wrap">
            <div class="title">登录</div>
            <div class="form-item" style="top: 7.75rem;">
              <el-form-item prop="username">
                <el-input
                  v-model="loginForm.username"
                  type="text"
                  auto-complete="off"
                  placeholder="账号"
                >
                  <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
                </el-input>
              </el-form-item>
            </div>
            <div class="form-item" style="top: 12.125rem;">
              <el-form-item prop="password">
                <el-input
                  v-model="loginForm.password"
                  type="password"
                  auto-complete="off"
                  placeholder="密码"
                  @keyup.enter.native="handleLogin"
                >
                  <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
                </el-input>
              </el-form-item>
            </div>
            <div class="form-item" style="top: 16.5rem;" v-if="captchaEnabled">
              <el-form-item prop="code">
                <el-input
                  v-model="loginForm.code"
                  auto-complete="off"
                  placeholder="请输入验证码"
                  style="width: 63%"
                  @keyup.enter.native="handleLogin"
                >
                </el-input>
                <div class="login-code">
                  <img :src="codeUrl" @click="getCode" class="login-code-img"/>
                </div>
              </el-form-item>
            </div>
            <div class="form-item" style="top: 20.875rem;">
              <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 1.5625rem 0px;">记住密码</el-checkbox>
            </div>
            <div class="form-item" style="top: 25.25rem;">
              <el-form-item style="width:100%;">
                <el-button
                  :loading="loading"
                  size="medium"
                  type="primary"
                  @click.native.prevent="handleLogin"
                >
                  <span v-if="!loading">登 录</span>
                  <span v-else>登 录 中...</span>
                </el-button>
                <div style="float: right;" v-if="register">
                  <router-link class="link-type" :to="'/register'">立即注册</router-link>
                </div>
              </el-form-item>
            </div>  
          </div>
        </el-form>
      </div>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-{{ curYear }} <a href="www.jiataoai.com">www.jiataoai.com</a> All Rights Reserved. 技术支持电话:13464006999</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
import moment from 'moment'
import { mapGetters } from 'vuex'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
      curYear: moment().year()
    };
  },
  computed: {
    ...mapGetters([
      'school',
    ]),
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  mounted() {//实例被挂载后调用
    this.getContainerWidth()
    window.addEventListener('resize', this.getContainerWidth)
  },
  beforeDestroy () {
    window.removeEventListener('resize', this.getContainerWidth)
  },
  methods: {
    getContainerWidth() {
      let that = this;
      let elementResizeDetectorMaker = require("element-resize-detector")
      let erd = elementResizeDetectorMaker()
      erd.listenTo(document.getElementById("login"), function (element) { 
      //监听div宽度变化
        let width = element.offsetWidth
        let height = element.offsetHeight
        console.log(width)
        console.log(height)
        that.$nextTick(function () {
          let fontSize = width / 120 + 'px';
          // let fontSize = '16px'
          let html = document.documentElement;
          html.style.fontSize = fontSize;
        })
      })
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.sys-title {
  font-family: Source Han Sans CN, Source Han Sans CN;
  font-weight: bold;
  font-size: 2.48rem;
  color: #FFFFFF;
}
.sys-welcome {
  font-family: Source Han Sans CN, Source Han Sans CN;
  font-weight: 400;
  font-size: 1.5rem;
  text-align: center;
  margin: 1.5rem 0 2.125rem 0;
  color: #90A7BD;
}
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/BG.png");
  background-size: 100% 100%; 
}
.title {
  margin: 0px auto 1.875rem auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 0.375rem;
  width: 31.25rem;
  padding: 0 1.5625rem 0.3125rem 1.5625rem;
  .form-wrap {
    width: 30rem;
    height: 31.9375rem;
    background-image: url("../assets/images/login.png");
    background-size: 100% 100%;
    position: relative;
    .title {
      position: absolute;
      width: 100%;
      text-align: center;
      top: 2.875rem;
      color: #13FFFC;
      padding-left: 0.625rem;
      font-size: 1.125rem;
    }
    .form-item {
      width: 23.5rem;
      position: absolute;
      left: 4.1875rem;
      .el-form-item {
        margin-bottom: 0;
      }
      .el-input__inner {
        color: #fff;
        background-color: rgba(0,0,0,0);
        border: 0;
      }
      .el-checkbox {
        color: #fff;
      }
      .el-checkbox__inner {
        border: 1px solid #067394;
        background-color: #003667;
      }
      .el-input--medium {
        font-size: 0.875rem;
        .el-input__inner {
          line-height: 1rem;
        }
      }
      .el-form-item--medium .el-form-item__content {
        line-height: 1rem;
      }
      .el-checkbox__label {
        font-size: 0.875rem;
        line-height: 1rem;
      }
      .el-checkbox__inner {
        width: 1rem;
        height: 1rem;
      }
      .el-button--medium {
        font-size: 0.875rem;
      }
    }
  }
  .el-input {
    height: 2.375rem;
    input {
      height: 2.375rem;
    }
  }
  .input-icon {
    height: 2.4375rem;
    width: 0.875rem;
    margin-left: 2px;
    color: #13FFFC;
  }
  .el-button--primary {
    width: 22.875rem;
    height: 3.125rem;
    background: linear-gradient(90deg, #1DAFAA 0%, #23FEF6 100%);
    border: 0;
    padding: 0;
  }
}
.login-tip {
  font-size: 0.8125rem;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 2.375rem;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 2.5rem;
  line-height: 2.5rem;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 0.75rem;
  letter-spacing: 1px;
  a {
    cursor: pointer;
  }
}
.login-code-img {
  height: 2.375rem;
}
.login-wrap {
  width: 100%;
  display: flex;
  justify-content: center;
  margin: auto 0;
  .login-logo {
    background-image: url('../assets/images/SCHOOL.png');
    width: 45.8125rem; 
    height:31.0625rem;
    background-size: 100% 100%;
  }
  .login-box {
    margin-left: 17.3125rem;
  }
}
</style>
